package com.gzz.console.service;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 *  服务路由
 *
 * 多态式
 *调用方法
 *     private ServiceRouter router;
 *     IService service = router.getService(type);
 *     service.print("hello world");
 */
@Component
public class ServiceRouter implements InitializingBean {

    private static final Map<Integer, IService> routerMap = Maps.newHashMap();

    @Autowired
    private List<IService> iServiceList;

    @Override
    public void afterPropertiesSet() throws Exception {
        for (IService service : iServiceList) {
            routerMap.put(service.getType(), service);
        }
    }

    public IService getService(Integer type) {
        return routerMap.get(type);
    }
}
